package com.vicki.music;

import android.app.TabActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.OnTabChangeListener;

public class MediaTabActivity extends TabActivity implements
		OnTabChangeListener, OnItemClickListener {

	private static String TAG_MediaTab = "MediaTabActivity";

	private TabHost tabhost;

	private ListView list_song;
	private ExpandableListView exp_list_artists;

	private MediaUpdateReceiver scanSdReceiver;
	private MediasChangeObserver mObserver = new MediasChangeObserver();

	public static BaseAdapter displayAdapter;
	public static ExpandableListAdapter exp_list_adapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabmain);

		initView();

		registerBroadcastRecevier();
		registerContentObservers();

		int tab = getTabHost().getCurrentTab();
		MediaUtils.scanSdCard(this, tab);
	}

	private void initView() {
		displayAdapter = new MediaAdapter(this);
		list_song = (ListView) findViewById(R.id.list_song);
		list_song.setAdapter(displayAdapter);
		list_song.setOnItemClickListener(this);
		
		exp_list_adapter = new ExpandAdapter(this);
		exp_list_artists = (ExpandableListView) findViewById(R.id.exp_list_album);
		exp_list_artists.setAdapter(exp_list_adapter);
		createTabs();
		
	}

	private void registerBroadcastRecevier() {
		IntentFilter intentfilter = new IntentFilter(
				Intent.ACTION_MEDIA_SCANNER_STARTED);
		intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
		intentfilter.addDataScheme("file");
		scanSdReceiver = new MediaUpdateReceiver();
		registerReceiver(scanSdReceiver, intentfilter);
	}

	/**
	 * Registers various content observers. The current implementation registers
	 * only a favorites observer to keep track of the favorites applications.
	 */
	private void registerContentObservers() {
		ContentResolver resolver = getContentResolver();
		resolver.registerContentObserver(
				android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				true, mObserver);
	}

	/**
	 * Receives notifications whenever the medias in sdcard have changed.
	 */
	private class MediasChangeObserver extends ContentObserver {
		public MediasChangeObserver() {
			super(new Handler());
		}

		@Override
		public void onChange(boolean selfChange) {
			// mediaAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(scanSdReceiver);
		super.onDestroy();
	}

	private void createTabs() {
		tabhost = getTabHost();

		tabhost.addTab(tabhost.newTabSpec(MediaUtils.Data_Artists)
				.setIndicator(MediaUtils.Data_Artists).setContent(
						R.id.exp_list_artist));

		tabhost.addTab(tabhost.newTabSpec(MediaUtils.Data_Albums)
				.setIndicator(MediaUtils.Data_Albums).setContent(
						R.id.list_song));

		tabhost.addTab(tabhost.newTabSpec(MediaUtils.Data_Songs)
				.setIndicator(MediaUtils.Data_Songs).setContent(
						R.id.list_song));

		tabhost.addTab(tabhost.newTabSpec(MediaUtils.Data_Playlists)
				.setIndicator(MediaUtils.Data_Playlists).setContent(
						R.id.list_song));

		tabhost.setOnTabChangedListener(this);

		getTabHost().setCurrentTab(2);
	}

	@Override
	public TabWidget getTabWidget() {
		Log.i(TAG_MediaTab, "getTabWidget()");
		return super.getTabWidget();
	}

	@Override
	public void onContentChanged() {
		super.onContentChanged();
		Log.i(TAG_MediaTab, "onContentChanged()");

	}

	@Override
	public void onTabChanged(String tabId) {
		Log.i(TAG_MediaTab, "onTabChanged() :" + tabId);
		MediaUtils utils = MediaUtils.getInstense();
		utils.bind_data_adapter(this, tabId);
		if (!MediaUtils.Data_Artists.equals(tabId)) {
			exp_list_artists.setVisibility(View.GONE);
		}else{
			exp_list_artists.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Log.i(TAG_MediaTab, "onItemClick() ");
		Intent intent = new Intent(this, MediaPlayDetail.class);
		startActivity(intent);
	}

}
